package emd24.telstudyresources;

import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * This class represents a question to be asked in the quiz.
 * 
 * @author Evan Dyke
 *
 */
public class Question implements Parcelable {
	public final static int NUM_ANSWERS = 5;
	private String questionText;
	private String solutionText;
	private int correctAnswer;
	private int selectedAnswer;
	private int questionNumber;
	boolean marked;
	/**
	 * Creates a question with fields specified in the constructor. This
	 * constructor will primarily be used for debugging
	 * 
	 * @param question String representing question
	 * @param answers String[] with answers to the question
	 * @param correctAnswer array index of correct answer
	 */
	public Question(String question, int correctAnswer){
		this.questionText = question;
		this.correctAnswer = correctAnswer;
		this.selectedAnswer = -1;
		this.marked = false;
	}
	/**
	 * Creates a question from a JSON Object.
	 * 
	 * @param question JSONObject representing question
	 * @param radnomize boolean whether to randomize answers
	 * @throws JSONException 
	 */
	public Question(JSONObject question, int number) throws JSONException{
		this.questionText = question.getString("questionText");
		this.solutionText = question.getString("solutionText");
		this.correctAnswer = question.getInt("correctAnswer");
		this.selectedAnswer = -1;
		this.questionNumber = number;		
		// Assign answers to questions
	}
	/**
	 * Creates a question from a Parcel.
	 * 
	 * @param source Parcel to be created from.
	 */
	public Question(Parcel source) {
		this.questionText = source.readString();
		this.solutionText = source.readString();
		this.correctAnswer = source.readInt();
		this.selectedAnswer = source.readInt();
		this.questionNumber = source.readInt();
		this.marked = (source.readByte() == 1);
	}
	/**
	 * @return the questionText
	 */
	public String getQuestionText() {
		return questionText;
	}
	/**
	 * @param questionText the questionText to set
	 */
	public void setQuestionText(String questionText) {
		this.questionText = questionText;
	}
	/**
	 * @return the correctAnswer
	 */
	public int getCorrectAnswer() {
		return correctAnswer;
	}
	/**
	 * @param correctAnswer the correctAnswer to set
	 */
	public void setCorrectAnswer(int correctAnswer) {
		this.correctAnswer = correctAnswer;
	}
	/**
	 * @return the selectedAnswer
	 */
	public int getSelectedAnswer() {
		return selectedAnswer;
	}
	/**
	 * Sets the answer that the user chose.
	 * 
	 * @param selectedAnswer index of selected answer.
	 */
	public void setSelectedAnswer(int selectedAnswer) {
		this.selectedAnswer = selectedAnswer;
	}
	/**
	 * @return the marked
	 */
	public boolean isMarked() {
		return marked;
	}
	/**
	 * @param marked the marked to set
	 */
	public void setMarked(boolean marked) {
		this.marked = marked;
	}
	/**
	 * @return the questionNumber
	 */
	public int getQuestionNumber() {
		return questionNumber;
	}
	/**
	 * @param questionNumber the questionNumber to set
	 */
	public void setQuestionNumber(int questionNumber) {
		this.questionNumber = questionNumber;
	}
	/**
	 * @return the solutionText
	 */
	public String getSolutionText() {
		return solutionText;
	}
	/**
	 * @param solutionText the solutionText to set
	 */
	public void setSolutionText(String solutionText) {
		this.solutionText = solutionText;
	}
	/**
	 * Checks to see if the question was answered correctly.
	 * 
	 * @return boolean representing if answer was correct.
	 */
	public boolean isCorrect(){
		return (this.correctAnswer == this.selectedAnswer);
	}
	public static final Parcelable.Creator<Question> CREATOR = 
			new Parcelable.Creator<Question>() {

		@Override
		public Question createFromParcel(Parcel source) {
			return new Question(source);
		}

		@Override
		public Question[] newArray(int size) {
			return new Question[size];
		}
	};
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.questionText);
		dest.writeString(this.solutionText);
		dest.writeInt(this.correctAnswer);
		dest.writeInt(this.selectedAnswer);
		dest.writeInt(this.questionNumber);
		dest.writeByte((byte) (this.marked ? 1 : 0));     //if myBoolean == true, byte == 1	
	}
}
